Hi there i need to get from my firestore db a list of items like this
items={{
'2012-05-22': [{name: 'item 1 - any js object'}],
'2012-05-23': [{name: 'item 2 - any js object', height: 80}],
'2012-05-24': [],
'2012-05-25': [{name: 'item 3 - any js object'}, {name: 'any js object'}]
}}
So this is my code to do it but isnt working
const getUserEvents = () => {
const FSquery = query( collection( FSdb, `usertasks/${user.uid}/events`) )
const unsubscribe = onSnapshot( FSquery, ( querySnapshot ) => {
// get all documents (using the date as id) from user's events collection
let eventData = {}
querySnapshot.forEach( (doc) => {
console.log("testing id", doc.id)
let arr = []
// the id of the document is the date for the event
eventData[ doc.id ] = arr
console.log("testing array", arr)
console.log("testing eventdata", eventData)
})
// now we get the collection of events for each day
let eventsOfTheDay = []
const eventKeys = Object.keys( eventData )
eventKeys.map( async (eventDate) => {
const events = await getDocs( FSdb, `users/${user.uid}/events/${eventDate}/items`)
events.forEach( (doc) => {
let event = doc.data()
event.id = doc.id
eventsOfTheDay.push( event )
})
eventData[ eventDate ] = eventsOfTheDay
console.log("this are the eventsoftheday", eventsOfTheDay)
})
// now we can set it into a state that can be passed to components to be consumed by the calendar
// eg setEvents( eventData )
})
}
I dont get any value from eventsoftheday not even the consloge string "this is eventsoftheday" Thanks i get this error [Unhandled promise rejection: FirebaseError: Expected type 'ba', but it was: a custom Oa object]
The getDocs function takes a Query as the only parameter but you are passing two.
const events = await getDocs(FSdb, `users/${user.uid}/events/${eventDate}/items`)
Try changing the above line to like this:
const events = await getDocs(
collection(FSdb, `users/${user.uid}/events/${eventDate}/items`)
);
// ^^^
// add collection